//
//  LCMapViewController.m
//  YiJun
//
//  Created by liangrongchang on 2017/4/20.
//  Copyright © 2017年 Rochang. All rights reserved.
//

#import "LCMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "LCHomeController.h"

@interface LCMapViewController ()<MAMapViewDelegate>

@property (nonatomic, weak) MAMapView *mapView;

@end

@implementation LCMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = Language(@"行驶轨迹");
    [AMapServices sharedServices].enableHTTPS = YES;
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;
    [self.view addSubview:mapView];
    self.mapView = mapView;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSArray <CLLocation *>*locations = [LCHomeController getLocationArray];
    if (!locations.count) {
        return;
    }
    CLLocationCoordinate2D commonPolylineCoords[locations.count];
    for (int i = 0; i < locations.count; i++) {
        CLLocationCoordinate2D coordinate = locations[i].coordinate;
        commonPolylineCoords[i] = coordinate;
    }
    [_mapView setCenterCoordinate:commonPolylineCoords[0]];
    
    //构造折线对象
    MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:locations.count];
    //在地图上添加折线对象
    [_mapView addOverlay: commonPolyline];
    [_mapView setVisibleMapRect:commonPolyline.boundingMapRect animated:YES];
}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
    if ([overlay isKindOfClass:[MAPolyline class]]){
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        polylineRenderer.lineWidth    = 4.f;
        polylineRenderer.strokeColor  = [UIColor redColor];
        polylineRenderer.lineJoinType = kMALineJoinRound;
        polylineRenderer.lineCapType  = kMALineCapRound;
        return polylineRenderer;
    }
    return nil;
}


@end
